package com.zhangkang.stock.stocklib.view

import android.graphics.Canvas
import android.graphics.Paint

/**
 * @author : Android-张康
 * created on: 2023/5/9 18:52
 * description:
 */
object CanvasTools {


    /**
     * 将文字绘制中竖直的中间
     *
     * @return 绘制文字的宽度
     */
    internal fun drawTextInVerticalCenter(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopY: Float,
        text: String,
        textPaint: Paint,
        rectPool: RectPool
    ): Int {
        val textBoundsRect = rectPool.acquire()
        textPaint.getTextBounds(text, 0, text.length, textBoundsRect)
        val textY =
            startY + (stopY - startY - textBoundsRect.height()) / 2 + textBoundsRect.height()
        canvas.drawText(text, startX, textY, textPaint)
        rectPool.release(textBoundsRect)
        return textBoundsRect.width()
    }

    /**
     * 绘制指标名称
     */
    internal fun drawIndicatorName(
        canvas: Canvas,
        rectLeft: Float,
        startY: Float,
        stopY: Float,
        linePaint: Paint,
        textPaint: Paint,
        rectPool: RectPool,
        indicatorName: String,
        indexNameConfig: IndexNameConfig
    ) {
        val rectRight = rectLeft + indexNameConfig.width
        val rectTop = startY + (stopY - startY - indexNameConfig.height) / 2
        val rectBottom = rectTop + indexNameConfig.height
        //绘制背景
        linePaint.style = Paint.Style.FILL
        linePaint.color = indexNameConfig.backgroundColor
        canvas.drawRoundRect(
            rectLeft,
            rectTop,
            rectRight,
            rectBottom,
            indexNameConfig.radius,
            indexNameConfig.radius,
            linePaint
        )
        //绘制描边
        linePaint.style = Paint.Style.STROKE
        linePaint.strokeWidth = indexNameConfig.strokeWidth
        linePaint.color = indexNameConfig.strokeColor
        canvas.drawRoundRect(
            rectLeft,
            rectTop,
            rectRight,
            rectBottom,
            indexNameConfig.radius,
            indexNameConfig.radius,
            linePaint
        )
        //绘制文字
        textPaint.textSize = indexNameConfig.textSize
        textPaint.color = indexNameConfig.textColor
        val textBoundsRect = rectPool.acquire()
        textPaint.getTextBounds(indicatorName, 0, indicatorName.length, textBoundsRect)
        val height = stopY - startY
        val textHeight = textBoundsRect.height()
        val textTop = startY + (height - textHeight) / 2
        val textX = rectLeft + indexNameConfig.textMarginStart
        val textY = textTop + textHeight
        canvas.drawText(indicatorName, textX, textY, textPaint)
        //绘制箭头
        indexNameConfig.arrowDropDownBitmap?.let {
            val bitmapTop = startY + (height - it.height) / 2
            canvas.drawBitmap(
                it,
                rectLeft + indexNameConfig.arrowDropDownMarginStart,
                bitmapTop,
                linePaint
            )
        }
        rectPool.release(textBoundsRect)
    }

}